﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace GameLogic
{
	public class GameMain
	{
		private List<ITimeDependant> timeDependants = new List<ITimeDependant>();
		private List<IRepresentable> representables = new List<IRepresentable>();

		private IRepresentable selectedObject = null;

		public GameMain()
		{
			Swordsman swordman = new Swordsman();
			swordman.setPosition(new Position2D(500, 500));
			addUnit(swordman);

			Barracks barracks = new Barracks(this);
			barracks.setPosition(new Position2D(250, 350));
			addBuilding(barracks);

			Swordsman swordman2 = new Swordsman();
			swordman2.setPosition(new Position2D(400, 300));
			addUnit(swordman2);
		}

		public void addUnit(Unit unit)
		{
			representables.Add(unit);
			timeDependants.Add(unit);
		}

		public void addBuilding(Building building)
		{
			representables.Add(building);
			timeDependants.Add(building);
		}

		public List<IRepresentable> getRepresentableContent()
		{
			return representables;
		}

		public void update()
		{
			for (int i = 0; i < timeDependants.Count; i++ )
			{
				timeDependants[i].update();
			}
		}

		public void selectObject(IRepresentable selectedObject)
		{
			if (this.selectedObject != null) this.selectedObject.unselect();

			this.selectedObject = selectedObject;
			selectedObject.select();
		}

		public void unselectAll()
		{
			if (this.selectedObject != null) this.selectedObject.unselect();
			this.selectedObject = null;
		}

		public void moveUnitTo(float x, float y)
		{
			if (selectedObject == null) return;

			if (selectedObject is Unit)
			{
				((Unit)selectedObject).moveTo(x, y);
			}
		}

		public void executeOption(Option option)
		{
			option.ExecuteAction((Building)selectedObject);
		}
	}
}
